<template>
    <LModalForm 
        :visible="visible"
        :confirmLoading="confirmLoading" 
        :title="title" 
        ref="dictItemModalForm" 
        :formAttributes="formAttributes" 
        @handleOk="handleOk" 
        @handleCancel="handleCancel"
        @clickEventHandler="clickEventHandler"
        @changeEventHandler="changeEventHandler"></LModalForm>

</template>

<script>
import LModalForm from "@/components/lt/form/LModalForm"
import pick from 'lodash.pick'
import request from '@/utils/request'
import defaultSettings from '@/config/defaultSettings'

export default {
    name:'dictItemEditForm',
    components:{
        LModalForm
    },
    data(){
        return{
            defaultSettings,
            title:'详情',
            visible:false,
            confirmLoading: false,
            model:{},
            formAttributes:{
              type:'',//collapse,tabs
              formItems:[
                {
                  title:'标签一',
                  columns:1,
                  fields:[
                    {field:'itemText',label:'显示文本',component:'input',rules: [{required: true, message: '请输入显示文本'}]},
                    {field:'itemValue',label:'数据值',component:'input',rules: [{required: true, message: '请输入数据值'}]},
                    {field:'itemFilter',label:'过滤值',component:'input'},
                    {field:'itemUse',label:'是否使用',component:'select',rules: [{ required: true, message: '请选择是否使用!' }],dictCode:defaultSettings.数据字典.是否,initialValue:'1'},
                    {field:'itemOrder',label:'数据排序',component:'inputNumber'},
                  ]
                },
              ]

            }


        }
    },
    methods:{
        add () {
            this.edit({});
        },
        edit (record) {
            this.visible = true;
            this.model = Object.assign({}, record);
            this.$nextTick(() => {
                this.$refs.dictItemModalForm.getForm().setFieldsValue(pick(this.model,'itemText','itemValue','itemFilter','itemUse','itemOrder'))
            })
        },
        handleOk(){
            const that = this;
            this.$refs.dictItemModalForm.getForm().validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    let formData = Object.assign(this.model, values);
                 
                    request({url: '/SysDictItemController/save',method: 'post',data: formData}).then((res)=>{
                        if(res.success){
                            that.$message.success(res.message);
                        }else{
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        that.close()
                    })
                    
                }
            })
        },
        handleCancel(){
            this.visible=false
        },
        close () {
            this.$emit('close');
            this.visible = false;
        },
         /**
         * 表单内容发生变化
         */
        changeEventHandler(changeItem){
     
        },
        clickEventHandler(field,form){
            
        }
    },
    created(){
        
    },
}
</script>